IF EXISTS(SELECT name FROM sysobjects
      WHERE name = 'pInOutProdPaper_Get' AND type = 'P')
   DROP PROCEDURE pInOutProdPaper_Get
GO

CREATE PROCEDURE dbo.pInOutProdPaper_Get
	@type SMALLINT
AS


/*______________________ CONSTANT DECLARATIONS _________________________*/

  

/*______________________ VARIABLE DECLARATIONS _________________________*/

  
/*_______________________________ INIT _________________________________*/


/*________________________ VALIDATE PARAMETERS _________________________*/
	
/*_______________________________ MAIN _________________________________*/
	
	
	SELECT 
		InOutProdPaper.Id, 
		InOutProdPaper.[Date],
		TotalMoney = (SELECT SUM(Amount * InputPrice) FROM InOutProdDetail
						WHERE InOutProdPaperId = InOutProdPaper.Id) ,
		TotalAmount = (SELECT SUM(Amount) FROM InOutProdDetail
						WHERE InOutProdPaperId = InOutProdPaper.Id) ,
		RowNumber = ROW_NUMBER() OVER(ORDER BY InOutProdPaper.Date),
		InOutProdPaper.FromShopId,
		InOutProdPaper.ToShopId,
		FromShopName = FromShop.Name,
		ToShopName = ToShop.Name
	 FROM 
		InOutProdPaper
	 LEFT JOIN Shop FromShop
		ON InOutProdPaper.FromShopId = FromShop.Id
		
	 LEFT JOIN Shop ToShop
		ON InOutProdPaper.ToShopId = ToShop.Id
		
	 WHERE Type = @type

/*___________________________ ERROR HANDLER ____________________________*/



